$(document)
    .ready(
        function ()
        {
            $("#from").datepicker(
            {
                defaultDate: "-1M",
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#to").datepicker("option", "minDate", selectedDate);
                }
            });
            $("#to").datepicker(
            {
                changeMonth: true,
                dateFormat: 'dd/mm/yy',
                changeYear: true,
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#from").datepicker("option", "maxDate", selectedDate);
                }
            });

            $('#myModal').modal();
            $("#from_modal").datepicker(
            {
                defaultDate: "-1M",
                changeMonth: true,
                dateFormat: 'dd/mm/yy',
                changeYear: true,
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#to_modal").datepicker("option","minDate", selectedDate);
                }
            });
            $("#to_modal").datepicker(
            {
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy',
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#from_modal").datepicker("option","maxDate", selectedDate);
                }
            });

            $("#establecer-modal").click(function ()
            {
                if ($("#from_modal").val() == "" || $("#to_modal").val() == "")
                {
                    $('#alerta').show();
                }
                else
                {
                    $("#from").val($("#from_modal").val());
                    $("#to").val($("#to_modal").val());
                    $('#myModal').modal('hide');
                    generarReporte();
                }
            });

            $("#Grid").jqGrid(
            {
                url: '/getReporte11?from=' + $('#from').val() + '&to=' + $('#to').val(),
                datatype: 'json',
                mtype: 'GET',
                loadonce: true,
                ajaxGridOptions:
                {
                    contentType: 'application/json; charset=utf-8'
                },
                defaults:
                {
                    recordtext: "Páh. {0} - {1} de {2}",
                    emptyrecords: "No existen datos",
                    loadtext: "Cargando...",
                    pgtext: "Pág {0} de {1}"
                },
                colNames: ['Nombre de Cliente',
                    'Numero de Proyectos'
                ],
                colModel: [
                {
                    name: 'cliente',
                    label: 'Cliente',
                    width: 250
                },
                {
                    name: 'nProyectos',
                    label: 'Proyectos',
                    width: 250
                }],
                rowNum: 10,
                rowList: [10, 20],
                sortname: 'name',
                sortorder: "asc",
                pager: "#Pager",
                viewrecords: true,
                gridview: true,
                width: 850,
                height: 231,
                rownumbers: true,
                caption: 'CARTERA DE CLIENTES CON MAYOR NÚMERO DE PROYECTOS',
                footerrow: true,
                // onPaging: function () {
                // generarReporte();
                // return 'stop';
                // },
                loadComplete: function ()
                {
                    var $self = $(this),
                        sum = $self
                            .jqGrid("getCol",
                                "nProyectos", false,
                                "sum");

                    $self.jqGrid("footerData", "set",
                    {
                        cliente: "Total:",
                        nProyectos: sum
                    });
                }

            })

            jQuery("#Grid").jqGrid('navGrid', '#Pager',
            {
                edit: false,
                add: false,
                del: false,
                search: false
            });
        });


function generarReporte()
{
    var grid = $("#Grid");
    var url = '/getReporte11?from=' + $('#from').val() + '&to=' + $('#to').val()
    $("#Grid").jqGrid('setGridParam',
    {
        url: url
    });
    $("#Grid").setGridParam(
    {
        datatype: 'json',
        page: 1
    }).trigger('reloadGrid');

    var $self = grid,
        sum = $self.jqGrid("getCol", "nProyectos", false, "sum");

    $self.jqGrid("footerData", "set",
    {
        cliente: "Total: ",
        nProyectos: sum
    });
    
	return false;

}

function restablecer()
{
    $('#from').val("");
    $('#to').val("");
    generarReporte();
    
	return false;
}

function comeindex() {
	window.open('/index','_self',false);
}